package cn.com.elite;

import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class EndPXService {
	private static final Log log=LogFactory.getLog(EndPXService.class);
	
	public String endPXService(DataSource dataSource,String ep_id,Document doc,String HandleGroup_ID,String Staff_ID)throws Exception{
		String ReturnXml="";
		Element LearnHistoryMainIDEle=(Element)doc.selectSingleNode("/Root/LearnHistoryMainID");
		if(LearnHistoryMainIDEle!=null){
			String LearnHistoryMainID=LearnHistoryMainIDEle.getText();
			if(LearnHistoryMainID!=null){
				Element ListIDEle=(Element)doc.selectSingleNode("/Root/ListID");
				if(ListIDEle!=null){
					String ListID=ListIDEle.getText();
					ReturnXml=endPXServiceImpl(dataSource,ep_id,LearnHistoryMainID,ListID);
				}else{
					throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamResultMainIDEle==null");
				}
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle==null");
		}
		return ReturnXml;
	}

	public String endPXServiceImpl(DataSource dataSource,String ep_id,String LearnHistoryMainID,String ListID)throws Exception{
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String EndTime=df.format(new Date());
		String W_LearnHistoryMain="W_LearnHistoryMain_"+ep_id;
		String W_LearnHistoryList="W_LearnHistoryList_"+ep_id;
		String sql1="update "+W_LearnHistoryMain+" set LearnEndTime=? where LearnHistoryMainID=? and ListID=?";
		int[] types1=new int[]{Types.TIMESTAMP,Types.VARCHAR,Types.VARCHAR};
		Object[] params1=new Object[]{EndTime,LearnHistoryMainID,ListID};
		String sql2="update "+W_LearnHistoryList+" set LearnEndTime=? where LearnHistoryMainID=? and ListID=?";
		int[] types2=new int[]{Types.TIMESTAMP,Types.VARCHAR,Types.VARCHAR};
		Object[] params2=new Object[]{EndTime,LearnHistoryMainID,ListID};
		util.UpdatePersonUseExecute(sql1, types1, params1, dataSource);
		util.UpdatePersonUseExecute(sql2, types2, params2, dataSource);
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("Result").addText("1");
		String ReturnXml=result.asXML();
		return ReturnXml;
		
	}

}
